package com.poobo.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.poobo.base.impl.BaseDaoImpl;
import com.poobo.dao.IResourcesDao;
import com.poobo.entity.TbResources;
import com.poobo.util.pager.Page;
import com.poobo.util.str.StrUtil;

@Repository
public class ResourcesDaoImpl extends BaseDaoImpl implements IResourcesDao{

	@Override
	public TbResources queryResourcesById(Integer id) {
		return selectObjectById(TbResources.class, id);
	}

	@Override
	public Page queryResourcesPage(TbResources resources, Page page) {
		String hql = " from TbResources where 1=1 ";
		String countHql = " select count(*) from TbResources where 1=1 ";
		List<Object> params = new ArrayList<Object>();
		String orderBy = " order by id desc";
		StringBuffer where = new StringBuffer();
		if( resources != null ){
			if( !StrUtil.isEmpty(resources.getName()) && resources.getName() != "" ){
				where.append(" and name like ? ");
				params.add("%" + resources.getName() + "%");
			}
			if( resources.getType() != null ){
				where.append(" and type = ? ");
				params.add(resources.getType());
			}
			if( !StrUtil.isEmpty(resources.getLastName()) && resources.getLastName() != "" ){
				where.append(" and lastName like ? ");
				params.add("%" + resources.getLastName() + "%");
			}
		}
		page.setBaseHql(hql + where.toString() + orderBy);
		page.setCountHql(countHql + where.toString());
		page.setParam(params);
		Page ppPage = getPageByHql(page);
		return getPageByHql(page);
	}

	@Override
	public void deleteResourcesById(Integer id) {
		String hql = " delete from TbResources where id = "+ id +" ";
		delObjectByIds(hql);
	}

	@Override
	public void saveResources(TbResources resources) {
		saveOrUpdate(resources);
	}

	@Override
	public void editResources(TbResources resources) {
		update(resources);
	}

	@Override
	public List<TbResources> queryAll() {
		String hql = " from TbResources ";
		return (List<TbResources>) findAll(hql);
	}

	@Override
	public List<TbResources> queryAllGames() {
		String hql = " from TbResources where type=0 ";
		return (List<TbResources>) findAll(hql);
	}

	@Override
	public List<TbResources> queryAllOther() {
		String hql = " from TbResources where type=1 or type=2 ";
		return (List<TbResources>) findAll(hql);
	}

	@Override
	public List<TbResources> queryRsourcesByType(Short type) {
		String hql =" from TbResources where type="+type+"  ";
		return (List<TbResources>) findAll(hql);
	}

}
